package com.meringer.beans.ejb;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import com.meringer.entities.CarModel;

/**
 * Session Bean implementation class CarModelsEJB
 */

@Stateless(mappedName = "CarModelsEJB")
@LocalBean
public class CarModelsEJB{

	@PersistenceContext
	private EntityManager em;
	
    public CarModelsEJB() {
        // TODO Auto-generated constructor stub
    }
    public String test(){
    	return "test";
    }

	public List<CarModel> getCarModels() {
		TypedQuery<CarModel> queryProductsByName = em.createNamedQuery("CarModel.getCarModels", CarModel.class);
		List<CarModel> listCarModels = queryProductsByName.getResultList();
		System.out.println(listCarModels);
		return listCarModels;
	}

	public void removeCarModel(Integer modelId) {
		CarModel carModel = em.find(CarModel.class, modelId);
		em.remove(carModel);
		System.out.println("removed car model with ID " + modelId);
	}
	
	public CarModel getCarModelById(Integer modelId) {
		return em.find(CarModel.class, modelId);
	}
	
	public void updateCarModel(CarModel newCarModel) {
		System.out.println("update car model in ejb");
		em.merge(newCarModel);
	}
	
	public void addCarModel(CarModel newCarModel) {
		System.out.println("create car model in ejb");
		em.persist(newCarModel);
		System.out.println("NEWMODELID " + newCarModel.getModelID());
	}

}
